package com.wyz.saas.module.patrol.bean

import androidx.room.ColumnInfo
import androidx.room.Entity
import com.wyz.saas.common.room.AbstractAssignEntity
import com.wyz.saas.common.utils.StringUtils.getRandomString
import java.sql.Timestamp

@Entity(tableName = "table_patrol_checkpoint")
class PatrolCheckpoint : AbstractAssignEntity() {

    companion object {
        /**
         * 获取指定间隔和周期的打卡次数
         * @gap 间隔
         * @cycle 周期
         */
        fun getSize(gap: String, cycle: String): Long {
            return getSize(cycle) / getSize(gap)
        }

        fun getSize(tineUnit: String): Long {
            when (tineUnit) {
                "YEAR" -> return 365 * 86400L
                "QUARTER" -> return 365 / 4 * 86400L
                "MONTH" -> return 31 * 86400L
                "WEEK" -> return 7 * 86400L
                "DAY" -> return 86400L
                "HOUR" -> return 3600L
                "MINUTE" -> return 60L
                "SECOND" -> return 1L
            }
            return -1
        }

        fun getTimeSpace(
            cycle: String,
            temp: Timestamp? = null
        ): Array<Timestamp> {
            var curr = temp
            if (curr == null) {
                curr = Timestamp(System.currentTimeMillis())
            }
            val times = arrayOf(curr, curr)
            when (cycle) {
                "YEAR" -> {
                    times[0] = Timestamp(curr.year, 0, 1, 0, 0, 0, 0)
                    times[1] = Timestamp(curr.year, 11, 31, 23, 59, 59, 0)
                }
                "QUARTER" -> {
                    val start = curr.month / 3
                    times[0] = Timestamp(curr.year, start * 3, 1, 0, 0, 0, 0)
                    times[1] = Timestamp(curr.year, start * 3 + 2, 31, 23, 59, 59, 0)
                }
                "MONTH" -> {
                    times[0] = Timestamp(curr.year, curr.month, 1, 0, 0, 0, 0)
                    times[1] = Timestamp(curr.year, curr.month, 31, 23, 59, 59, 0)
                }
                "WEEK" -> {
                    val start_day = curr.date - curr.day // 1-31
                    times[0] = Timestamp(curr.year, curr.month, start_day, 0, 0, 0, 0)
                    times[1] = Timestamp(times[0].time + 7 * 86400000L)
                }
                "DAY" -> {
                    times[0] = Timestamp(curr.year, curr.month, curr.date, 0, 0, 0, 0)
                    times[1] = Timestamp(curr.year, curr.month, curr.date, 23, 59, 59, 0)
                }
                "HOUR" -> {
                    times[0] = Timestamp(curr.year, curr.month, curr.date, curr.hours, 0, 0, 0)
                    times[1] = Timestamp(curr.year, curr.month, curr.date, curr.hours, 59, 59, 0)
                }
                "MINUTE" -> {
                    times[0] =
                        Timestamp(curr.year, curr.month, curr.date, curr.hours, curr.minutes, 0, 0)
                    times[1] =
                        Timestamp(curr.year, curr.month, curr.date, curr.hours, curr.minutes, 59, 0)
                }
                "SECOND" -> {
                    times[0] = Timestamp(
                        curr.year,
                        curr.month,
                        curr.date,
                        curr.hours,
                        curr.minutes,
                        curr.seconds,
                        0
                    )
                    times[1] = Timestamp(
                        curr.year,
                        curr.month,
                        curr.date,
                        curr.hours,
                        curr.minutes,
                        curr.seconds,
                        999999999
                    )
                }
            }
            return times
        }
    }

    @ColumnInfo(name = "label")
    var label: String = "PatrolRanger_label" + getRandomString(6)

    @ColumnInfo(name = "comments")
    var comments: String? = null

    @ColumnInfo(name = "point")
    var point: String? = null// 空间坐标字符串

    @ColumnInfo(name = "buffer")
    var buffer: Double? = null

    @ColumnInfo(name = "times")
    var times: Int? = null

    @ColumnInfo(name = "gap")
    var gap: Long? = null

    @ColumnInfo(name = "cycle")
    var cycle: Long? = null


    override fun toString(): String {
        return "PatrolCheckpoint(id='$id', createAt=$createAt, updateAt=$updateAt, enabled=$enabled, deleted=$deleted, shared=$shared, assignToId=$assignToId, assignToPath=$assignToPath, assignToLabel=$assignToLabel, label=$label, comments=$comments, point=$point, buffer=$buffer, times=$times, gap=$gap, cycle=$cycle)"
    }
}